#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>


#define	ERROR	1 
#define	DIRNAME	"new_dir"

int main(int argc,char **argv)
{
	DIR* dirp ;
	struct dirent* direntp ;
	int fd = -1 ;
	char dir_name[] = DIRNAME ;
	if(mkdir(dir_name, 0755) < 0)
	{
		printf("make dir %s failed: %s\n", dir_name, strerror(errno));
		return ERROR ;
	
	}
	
	dirp = opendir(DIRNAME);
	if(dirp == NULL)
	{
		printf("open dir %s failed: %s\n", dir_name, strerror(errno)) ;
		goto cleanup ;
	}

	if(chdir(dir_name) < 0)
	{
		perror("change dirctery failed!\n") ;
		return ERROR ;
	}

	printf("change dir to %s suceeful!\n", dir_name) ;

	fd = creat("file.txt",0666) ;

	if(fd < 0)
	{
		perror("creat file failed!\n") ;
		goto cleanup ;
	}

	while( (direntp = readdir(dirp) ) != NULL)
	{
		printf("File file %s\n",direntp -> d_name );
	
	}
	





cleanup:
	closedir(dirp) ;
	close(fd) ;

	return 0 ;
}
